package com.atguigu.day20.single02;

public class LazyHungry {
    private static  LazyHungry instance;

    private LazyHungry() {
    }
    //todo 双if校验
    public static LazyHungry getInstance() throws InterruptedException {

        if (instance == null) {
            Thread.sleep(20);
            synchronized (LazyHungry.class) {
                if (instance == null) {
                    instance = new LazyHungry();
                }
            }
        }
        return instance;
    }


 /*   //线程安全的方法
    public static synchronized LazyHungry getInstance() throws InterruptedException {

        if (instance == null) {
            Thread.sleep(20);
            instance = new LazyHungry();
        }
        return instance;
    }*/


   /* //todo 有线程安全问题
    public static LazyHungry getInstance() throws InterruptedException {

        if (instance == null) {
            Thread.sleep(20);
            instance = new LazyHungry();
        }
        return instance;
    }*/



}

class LazyHungry2{

    private LazyHungry2(){}

    private static class AA{
        private static final LazyHungry2 INSTANCE = new LazyHungry2();
    }
    public static LazyHungry2 getInstance(){
        //只有使用了内部类的静态资源才会导致内部类加载
        return AA.INSTANCE;
    }


}

